Aggregation (HAS-A)

Passing an object of Class 1 as an argument of class 2 constructer


In [95]:
class A():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def addNums():
        self.b + self.c

class B():
    def __init__(self, d, e, A):
        self.d = d
        self.e = e
        self.A = A

    def addAllNums(self):
        x = self.d + self.e + self.A.b + self.A.c
        return x

objA = A("hi", 2, 6)
objB = B(5, 9, objA)
objB.addAllNums()


Out[95]:
22

Association (USES-A)

Passing object of class 1 as an argument of class 2 methods


In [91]:
class A():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def addNums():
        self.b + self.c

class B():
    def __init__(self, d, e):
        self.d = d
        self.e = e        

    def addAllNums(self, arg):
        x = self.d + self.e + arg.b + arg.c
        return x

objA = A("hi", 2, 6)
objB = B(5, 9)
objB.addAllNums(objA)


Out[91]:
22

Composition (PART-OF)

Object of class 1 is defined inside the constructor of class 2


In [94]:
class A():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def addNums():
        self.b + self.c

class B():
    def __init__(self, d, e):
        self.d = d
        self.e = e
        self.objA = A("hi", 2, 6)

    def addAllNums(self):
        x = self.d + self.e + self.objA.b + self.objA.c
        return x


objB = B(5, 9)
objB.addAllNums()


Out[94]:
22

Inheritance (IS-A)


In [102]:
class A():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def addNums():
        self.b + self.c

class B(A):
    def __init__(self, a, b, c, d, e):
#         A.__init__(self, a, b, c)
        super().__init__(a, b, c)
        self.d = d
        self.e = e
        

    def addAllNums(self):
        x = self.a + self.b + self.c + self.d + self.e
        return x


objB = B(1, 2, 3, 5, 9)
objB.addAllNums()


Out[102]:
20

Function overriding


In [111]:
class A():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def addNums(self):
        return self.b * self.c

class B(A):
    def __init__(self, a, b, c, d, e):
        super().__init__(a, b, c)
        self.d = d
        self.e = e
        
    def addNums(self):
        return self.d + self.e
    
    def check(self):
        print("Class B func:", self.addNums())
        print("Class B func:", super().addNums())


objB = B(1,2,3,5, 9)
objB.check()


Class B func: 14
Class B func: 6

There is no function overloading

Gives no error but only last defined function is executed


In [116]:
class A():
    def f1(self, x):
        return x
    def f1(self, x, y):
        return x, y


objA = A()
objA.f1(8,5)
# objA.f1(8)    # Gives error


Out[116]:
(8, 5)

In [121]:
# How to work with function overloading
class A():
    def f1(self,name = None):
        if name is None:
            return 5
        else:
            return name

objA = A()
print(objA.f1())
objA.f1(8)


5
Out[121]:
8